1   package com.iluwatar;
2   
3   import static org.junit.Assert.*;
4   
5   import org.junit.Before;
6   import org.junit.Test;
7   
8   /**
9    * This test case is responsible for testing our application by taking advantage
10   * of the Model-View-Controller architectural pattern.
11   */
12  public class FileSelectorPresenterTest {
13  
14  	/**
15  	 * The Presenter component.
16  	 */
17  	private FileSelectorPresenter presenter;
18  
19  	/**
20  	 * The View component, implemented this time as a Stub!!!
21  	 */
22  	private FileSelectorStub stub;
23  
24  	/**
25  	 * The Model component.
26  	 */
27  	private FileLoader loader;
28  
29  	/**
30  	 * Initializes the components of the test case.
31  	 */
32  	@Before
33  	public void setUp() {
34  		this.stub = new FileSelectorStub();
35  		this.loader = new FileLoader();
36  		presenter = new FileSelectorPresenter(this.stub);
37  		presenter.setLoader(loader);
38  	}
39  
40  	/**
41  	 * Tests if the Presenter was successfully connected with the View.
42  	 */
43  	@Test
44  	public void wiring() {
45  		presenter.start();
46  
47  		assertNotNull(stub.getPresenter());
48  		assertTrue(stub.isOpened());
49  	}
50  
51  	/**
52  	 * Tests if the name of the file changes.
53  	 */
54  	@Test
55  	public void updateFileNameToLoader() {
56  		String EXPECTED_FILE = "Stamatis";
57  		stub.setFileName(EXPECTED_FILE);
58  
59  		presenter.start();
60  		presenter.fileNameChanged();
61  
62  		assertEquals(EXPECTED_FILE, loader.getFileName());
63  	}
64  
65  	/**
66  	 * Tests if we receive a confirmation when we attempt to open a file that
67  	 * it's name is null or an empty string.
68  	 */
69  	@Test
70  	public void fileConfirmationWhenNameIsNull() {
71  		stub.setFileName(null);
72  
73  		presenter.start();
74  		presenter.fileNameChanged();
75  		presenter.confirmed();
76  
77  		assertFalse(loader.isLoaded());
78  		assertEquals(1, stub.getMessagesSent());
79  	}
80  
81  	/**
82  	 * Tests if we receive a confirmation when we attempt to open a file that it
83  	 * doesn't exist.
84  	 */
85  	@Test
86  	public void fileConfirmationWhenFileDoesNotExist() {
87  		stub.setFileName("RandomName.txt");
88  
89  		presenter.start();
90  		presenter.fileNameChanged();
91  		presenter.confirmed();
92  
93  		assertFalse(loader.isLoaded());
94  		assertEquals(1, stub.getMessagesSent());
95  	}
96  
97  	/**
98  	 * Tests if we can open the file, when it exists.
99  	 */
100 	@Test
101 	public void fileConfirmationWhenFileExists() {
102 		stub.setFileName("etc/data/test.txt");
103 		presenter.start();
104 		presenter.fileNameChanged();
105 		presenter.confirmed();
106 
107 		assertTrue(loader.isLoaded());
108 		assertTrue(stub.dataDisplayed());
109 	}
110 
111 	/**
112 	 * Tests if the view closes after cancellation.
113 	 */
114 	@Test
115 	public void cancellation() {
116 		presenter.start();
117 		presenter.cancelled();
118 
119 		assertFalse(stub.isOpened());
120 	}
121 }